<?php

/**
 * 订单操作
 */

namespace app\order\common;

class OrderCommon extends \app\member\common\MemberCommon {

    private $orderInfo = [];

    public function __construct() {
        parent::__construct();
        $orderNo = request('', 'order_no', 0);
        if (empty($orderNo)) {
            $this->error('订单号不正确!');
        }
        $target = target('order/Order');
        $orderInfo = $target->getWhereInfo([
            'order_no' => $orderNo
        ]);
        if($orderInfo['order_user_id'] <> $this->userInfo['user_id']) {
            $this->error('该订单无法操作!');
        }
        $this->orderInfo = $orderInfo;
    }

    /**
     * 取消订单
     */
    protected function cancel() {
        $orderInfo = $this->orderInfo;

        if($orderInfo['status_data']['action'] <> 'pay' && $orderInfo['status_data']['action'] <> 'parcel') {
            $this->error('无法取消该订单!');
        }
        $model = target('order/Order');
        $model->beginTransaction();
        $refund = false;
        if($orderInfo['status_data']['action'] == 'delivery') {
            $refund = true;
        }
        if(!target('order/Order', 'service')->cancelOrder($orderInfo['order_id'], $refund)) {
            $model->rollBack();
            $this->error(target('order/Order', 'service')->getError());
        }
        $model->commit();
        $this->success('取消订单成功!');
    }

    /**
     * 确认收货
     */
    protected function confirm() {
        $orderInfo = $this->orderInfo;
        if($orderInfo['status_data']['action'] <> 'receive') {
            $this->error('无法进行确认收货!');
        }
        $model = target('order/Order');
        $model->beginTransaction();
        if(!target('order/Order', 'service')->confirmOrder($orderInfo['order_id'])) {
            $model->rollBack();
            $this->error(target('order/Order', 'service')->getError());
        }
        $model->commit();
        $this->success('确认收货成功!');
    }

    /**
     * 查看快递
     */
    protected function delivery() {
        $this->pageInfo['name'] = '物流跟踪';
        $this->setMeta('物流跟踪');
        $this->setCrumb([
            [
                'name' => '物流跟踪',
                'url' => url()
            ]
        ]);
        $orderInfo = $this->orderInfo;
        $waybillLog = target('order/Order', 'service')->getWaybillLog($orderInfo['order_id']);
        $msg = '';
        if(empty($waybillLog)) {
            $msg = target('order/Order', 'service')->getError();
            $waybillLog = [];
        }
        $deliveryInfo = target('order/OrderDelivery')->getWhereInfo([
            'A.order_id' => $orderInfo['order_id']
        ]);
        $this->assign('deliveryInfo' , $deliveryInfo);
        $this->assign('orderInfo' , $orderInfo);
        $this->assign('msg' , $msg);
        $this->assign('logList', $waybillLog);
        $this->memberDisplay();
    }

    /**
     * 查看状态
     */
    protected function status() {
        $orderInfo = $this->orderInfo;
        $this->success([
            'order_status' => $orderInfo['order_status'],
            'order_complete_status' => $orderInfo['order_complete_status'],
            'pay_status' => $orderInfo['pay_status'],
            'parcel_status' => $orderInfo['parcel_status'],
            'comment_status' => $orderInfo['comment_status'],
            'delivery_status' => $orderInfo['delivery_status'],
        ]);


    }

}